package cn.com.lms.business.cc.controller;


import cn.com.lms.business.cc.persistence.entity.ExamExercise;
import cn.com.lms.business.cc.persistence.entity.ExamExerciseEditorVO;
import cn.com.lms.business.cc.service.CCExerciseService;
import cn.com.lms.business.cc.vo.ExamExerciseVO;
import cn.com.lms.business.examRewrite.service.ExerciseReWriteService;
import com.bnzj.core.persistence.query.CompareExpression;
import com.bnzj.core.persistence.query.CompareType;
import com.bnzj.core.persistence.query.IExpression;
import com.bnzj.core.persistence.query.QueryHelpBean;
import com.bnzj.core.rest.ResponseResult;
import com.bnzj.core.webmvc.util.WebMvcUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

@RestController
@RequestMapping("/cc/exam/exercise")
public class CCExerciseController {

    @Autowired
    CCExerciseService ccExerciseService;

    @Autowired
    ExamExerciseEditorVO examExerciseEditorVO;


    /**
     * 练习主页数据
     * @param request
     * @return
     */
    @GetMapping("/pageReWrite")
    public ResponseResult<Page<ExamExerciseVO>> Page(HttpServletRequest request){
        String keyWord = request.getParameter("KeyWord");
        Pageable pageable = WebMvcUtils.initPage(request, Sort.by(Sort.Direction.DESC, "ee.create_time"));
        QueryHelpBean queryHelpBean = CCExerciseService.getQueryHelpBean();
        queryHelpBean.setPageable(pageable);
        if(!StringUtils.isEmpty(keyWord)){
            IExpression build = CompareExpression.build("ee.exercise_name", CompareType.like, keyWord);
            queryHelpBean.addWhere(build);
        }
        IExpression build1 = CompareExpression.build("ee.is_delete", CompareType.equal, false);
        queryHelpBean.addWhere(build1);
        Page<ExamExerciseVO> all = ccExerciseService.findAll(queryHelpBean);
        return ResponseResult.success(all);
    }


    @RequestMapping("/oneReWrite/{id}")
    public ResponseResult<ExamExerciseVO> getById(@PathVariable("id") Long id){

        Optional<ExamExercise> examExerciseOptional = ccExerciseService.findById(id);
        ExamExercise examExercise = examExerciseOptional.get();
        BeanUtils.copyProperties(examExercise, examExerciseEditorVO);
        return null;
    }




}
